package com.ds.dsm.view.config.form.service;

import com.ds.common.JDSException;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.dsm.view.config.form.field.FieldFormBean;
import com.ds.dsm.view.config.form.field.group.*;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;

import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.form.CustomFormViewBean;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavGroupViewAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.dsm.view.field.FieldFormConfig;
import net.sf.cglib.beans.BeanMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping(path = "/dsm/view/config/form/field/combo/")
@MethodChinaName(cname = "字段配置", imageClass = "spafont spa-icon-c-comboinput")

public class FieldComboBoxService {


    @MethodChinaName(cname = "清空配置信息")
    @RequestMapping(method = RequestMethod.POST, value = "clearFieldForm")
    @APIEventAnnotation(callback = {CustomCallBack.Reload})
    public @ResponseBody
    ResultModel<Boolean> clearFieldForm(String serviceClassName, String methodName, String fieldname,String domainId, String viewInstId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            if (serviceClassName != null && !serviceClassName.equals("")) {
                ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(serviceClassName,viewInstId);
                MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
                CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
                formViewBean.getFieldConfigMap().remove(fieldname);
                DSMFactory.getInstance().getViewManager().reSetViewEntityConfig(serviceClassName,domainId, viewInstId);
            }
        } catch (JDSException e) {
            ErrorResultModel<Boolean> errorResult = new ErrorResultModel<Boolean>();
            errorResult.setErrdes(e.getMessage());
            result = errorResult;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return result;
    }


    @MethodChinaName(cname = "编辑字段配置信息")
    @RequestMapping(method = RequestMethod.POST, value = "updateFieldForm")
    @APIEventAnnotation(callback = {CustomCallBack.Reload}, bindMenu = CustomMenuItem.save)
    public @ResponseBody
    ResultModel<Boolean> updateFieldForm(@RequestBody FieldFormBean config) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            String className = config.getSourceClassName();
            if (className != null && !className.equals("")) {
                ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(className, config.getViewInstId());
                MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(config.getMethodName());
                CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
                FieldFormConfig fieldConfig = (FieldFormConfig) formViewBean.getFieldConfigMap().get(config.getFieldname());
                Map<String, Object> configMap = BeanMap.create(fieldConfig);
                configMap.putAll(BeanMap.create(config));
                Map<String, Object> widgetMap = BeanMap.create(fieldConfig.getWidgetConfig());
                widgetMap.putAll(BeanMap.create(config));
                DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
            }
        } catch (JDSException e) {
            ErrorResultModel<Boolean> errorResult = new ErrorResultModel<Boolean>();
            errorResult.setErrdes(e.getMessage());
            result = errorResult;
        }

        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldNav")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "字段信息")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldCustomGroup> getFieldNav(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldNumberNav")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "数字字段")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldNumberGroup> getFieldNumberNav(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldcombobox")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "复合框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldComboNav(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldinput")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "普通输入框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldInputGroup> getFieldinput(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "Fieldpassword")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "密码框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldInputGroup> getFieldpassword(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldcheckbox")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "单选框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldcheckbox(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldlistbox")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "下拉列表")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldlistbox(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldgetter")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "弹出字典")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldModuleGroup> getFieldgetter(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldhelpinput")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "下拉帮助")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldModuleGroup> getFieldhelpinput(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldbutton")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "按钮")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldbutton(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
}


    @RequestMapping(method = RequestMethod.POST, value = "Fielddropbutton")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "状态按钮")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFielddropbutton(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "Fieldcmdbox")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "自定义按钮组")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldcmdbox(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldcmd")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "自定义按钮")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldcmd(String sourceClassName,  String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldpopbox")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "弹出框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldpopbox(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldfile")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "文件")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldfile(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldtextarea")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "多行输入")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldtextarea(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldlabel")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "LABLE标签")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldlabel(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fielddate")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "日期框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFielddate(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "Fieldprofile")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "profile")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldprofile(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldimage")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "profile")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldimage(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldtext")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "文本")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldtext(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldtime")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "时间框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldtime(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldcolor")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "颜色选择")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldcolor(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldspin")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "数字滚动")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldNumberGroup> getFieldspin(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldcounter")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "左右滚动")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldcounter(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldcurrency")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "货币")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldcurrency(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldauto")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "默认类型")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldauto(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "Fieldnumber")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "数字")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldNumberGroup> getFieldnumber(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "Fieldsplit")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "分隔符")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldInputGroup> getFieldsplit(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldModuleNav")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation( caption = "子模块信息")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldModuleGroup> getFieldModuleNav(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }
}
